<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col>
                        <el-form-item label="" prop="status">
                            <el-select v-model="condition.search.status"
                                       placeholder="状态">
                                <el-option label="正常"
                                           value="1"></el-option>
                                <el-option label="过期"
                                           value="2"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="exportExcel">导出</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            :data="tableData"
            size="small"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="60">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="username"
                label="用户名">
            </el-table-column>
            <el-table-column
                prop="ip"
                label="IP地址">
            </el-table-column>
            <el-table-column
                prop="status"
                :formatter="formatStatus"
                label="状态">
            </el-table-column>
            <el-table-column
                prop="loginTime"
                label="登录时间">
            </el-table-column>
            <el-table-column
                prop="expiryTime"
                label="过期时间">
            </el-table-column>
            <el-table-column
                prop="lastAccessTime"
                label="最近访问时间">
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-dialog :title=addOrUpdateDialogTitle width="30%" :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="120px" size="small">
                <el-form-item label="商户ID">
                    <el-input v-model="addOrUpdateForm.merchantId"
                              placeholder="商户ID"></el-input>
                </el-form-item>
                <el-form-item label="用户ID">
                    <el-input v-model="addOrUpdateForm.userId"
                              placeholder="用户ID"></el-input>
                </el-form-item>
                <el-form-item label="登录用户名">
                    <el-input v-model="addOrUpdateForm.username"
                              placeholder="登录用户名"></el-input>
                </el-form-item>
                <el-form-item label="登录TOKEN">
                    <el-input v-model="addOrUpdateForm.token"
                              placeholder="登录TOKEN"></el-input>
                </el-form-item>
                <el-form-item label="登录IP地址">
                    <el-input v-model="addOrUpdateForm.ip"
                              placeholder="登录IP地址"></el-input>
                </el-form-item>
                <el-form-item label="登录IP地址位置">
                    <el-input v-model="addOrUpdateForm.ipLocation"
                              placeholder="登录IP地址位置"></el-input>
                </el-form-item>
                <el-form-item label="UA信息">
                    <el-input v-model="addOrUpdateForm.ua"
                              placeholder="UA信息"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-input v-model="addOrUpdateForm.status"
                              placeholder="状态"
                              oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                </el-form-item>
                <el-form-item label="设备信息">
                    <el-input v-model="addOrUpdateForm.deviceInfo"
                              placeholder="设备信息"></el-input>
                </el-form-item>
                <el-form-item label="登录时间">
                    <el-date-picker
                        v-model="addOrUpdateForm.loginTime"
                        type="datetime"
                        placeholder="登录时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        align="right">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="过期时间">
                    <el-date-picker
                        v-model="addOrUpdateForm.expiryTime"
                        type="datetime"
                        placeholder="过期时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        align="right">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="最近访问时间">
                    <el-date-picker
                        v-model="addOrUpdateForm.lastAccessTime"
                        type="datetime"
                        placeholder="最近访问时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        align="right">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="主动登出时间">
                    <el-date-picker
                        v-model="addOrUpdateForm.loginOutTime"
                        type="datetime"
                        placeholder="主动登出时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        align="right">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addOrUpdateDialogVisible = false" size="small">取 消</el-button>
                <el-button @click="addOrUpdateConfirm" type="primary" size="small">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {addUserLoginLog, deleteUserLoginLog, getUserLoginLogByPage, updateUserLoginLog} from '@/api/api'

export default {
    name: "UserLoginLog",
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {}
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        userId: String,
        size: {
            type: String,
            default: 'small'
        },
    },
    watch: {
        userId: function (val, oldVal) {
            this.userId = val;
            this.getData();
        }
    },
    methods: {
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatStatus: function (row, column, cellValue) {
            if (cellValue == '1') {
                return "正常";
            } else if (cellValue == '2') {
                return "过期";
            } else {
                return "未知";
            }
        },
        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addUserLoginLog(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        update: function () {
            updateUserLoginLog(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteUserLoginLog({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取表格数据
        getData: function () {
            this.condition.search.userId = this.userId;
            getUserLoginLogByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
            });
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
